$(document).ready(function () {
    var loc_sel = false;

    var map = initialMap(function(){
        map.panBy($("#map-body").width()/2, $("#map-body").height()/2);
    });

    var geoc = new BMap.Geocoder();
    map.addEventListener("click", function(e){
        loc_sel = true;
        map.clearOverlays();
        var marker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));
        map.addOverlay(marker);
        $("#xpos").val(e.point.lng);
        $("#ypos").val(e.point.lat);
        geoc.getLocation(e.point, function(rs){
            var addComp = rs.addressComponents;
            $("#location").val((addComp.province + (addComp.city==addComp.province?"":addComp.city) + addComp.district + addComp.street + addComp.streetNumber));
        });
    });

    $("#submitbtn").click(function () {
        if ($("#name").val()=="" || $("#username").val()==""){
            alert("商家名称和用户名不能为空");
            return;
        }
        if (!loc_sel) {
            alert("请先在地图上标记位置");
            return;
        }

        $("form").submit();
    });
});

